import { Obj } from "./obj.js";
import { FootHoldTree } from "./FootHoldTree.js";
import { Background } from "./MapBackground.js";
import { LadderRopes } from "./LadderRopes.js";
import { Tile } from "./tile.js";
import { MapHelper } from "./MapHelper/MapHelper.js";

export class MapleStoryMap {
  constructor(resource, stage, backStage) {
    this.resource = resource
    this.stage = stage;
    this.backStage = backStage;

    this.mapInfo = null;
    this.Map = null;

    this.background = new Background(resource, backStage);
    this.obj = new Obj(resource, stage);
    this.tile = new Tile(resource, stage);
    this.mapHelper = new MapHelper(resource, stage);

    /**
     * @public
     */
    this.footholdtree = null;
    /**
     * @public
     */
    this.ladderRopes = null;
  }

  /**
   * 
   * @param {string} mapSort 
   * @param {string} mapId
   * @public 
   * 这个函数表示切换和加载
   */
  async load(mapSort, mapId) {
    await this.resource.loadMap(mapSort, mapId)
    const Map = this.Map = this.resource.Content.Map.Map[mapSort][mapId];

    // this.footholdtree = new FootHoldTree(Map.foothold);
    this.footholdtree = Map.foothold;
    this.ladderRopes = new LadderRopes(Map.ladderRope);
    await this.mapHelper.load();
    await this.obj.load(Map);
    await this.tile.load(Map);
    await this.background.load(Map);
  }

  update() {
    this.background.update()
  }

  /**
   * @public
   */
  draw() {
    this.background.draw();
    for (let i = 0; i <= 7; i++) {
      this.obj.draw(i);
      this.tile.draw(i)
    }
    this.mapHelper.portal.draw(this.Map.portal);
  }

  /**
   * @public
   */
  destroy() {
    this.obj.destroy();
    this.tile.destroy();
    this.background.destroy();
    this.mapHelper.portal.removeAll();
  }
}